package com.qianye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianye.entity.Category;
import com.qianye.entity.Customer;
import com.qianye.entity.Expressinfo;
import com.qianye.entity.Rejectdetails;
import com.qianye.mapper.CategoryMapper;
import com.qianye.mapper.CustomerMapper;
import com.qianye.mapper.ExpressInfoMapper;
import com.qianye.mapper.RejectdetailsMapper;
import com.qianye.service.CustomerService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ExpressInfoMapper expressInfoMapper;
    @Autowired
    private RejectdetailsMapper rejectdetailsMapper;


    @Override
    public int addCustomer(Customer customer) {
        Md5Hash hash = new Md5Hash(customer.getCpassword());
        String password = hash.toHex();
        customer.setCpassword(password);
        customer.setCstatus(1);
        return customerMapper.insert(customer);
    }

    @Override
    public List<Category> findAllCate() {
        return categoryMapper.selectList(null);
    }

    @Override
    public List<Expressinfo> addressseeAll(String username) {
        List<Expressinfo> expressinfoList = expressInfoMapper.addresseeAll(username);
        return expressinfoList;
    }

    @Override
    public void reciverExpress(Integer kid) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String reciveradate = sdf.format(date);
        Date date1 = sdf.parse(reciveradate);
        Expressinfo expressinfo = new Expressinfo();
        expressinfo.setEstimatedDelivery(date1);
        expressinfo.setKid(kid);
        expressinfo.setExpressStatus(5);
        expressInfoMapper.updateById(expressinfo);
    }

    @Override
    public void addRejectDetails(Rejectdetails rejectdetails) {
        Expressinfo expressinfo = new Expressinfo();
        expressinfo.setKid(rejectdetails.getEid());
        expressinfo.setExpressStatus(6);
        expressInfoMapper.updateById(expressinfo);
        rejectdetailsMapper.insert(rejectdetails);
    }

    @Override
    public Customer findCustomerByName(String username) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("cusername",username);
        Customer customer = customerMapper.selectOne(wrapper);
        return customer;
    }

    @Override
    public List<Expressinfo> findOwnerSent(String username) {
        QueryWrapper<Expressinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("express_owner_name",username);
        wrapper.eq("express_status",6);
        List<Expressinfo> expressinfoList = expressInfoMapper.selectList(wrapper);
        return expressinfoList;
    }

    @Override
    public List<Expressinfo> findMyReceive(String username) {
        QueryWrapper<Expressinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("express_sender_name",username);
        List<Expressinfo> expressinfoList = expressInfoMapper.selectList(wrapper);
        return expressinfoList;
    }
}
